package com.gg.hj.service.impl;

import com.gg.common.mapper.DFileMapper;
import com.gg.common.mapper.SCellMapper;
import com.gg.common.mapper.SPayDetailsMapper;
import com.gg.common.mapper.SPayMapper;
import com.gg.common.pojo.DFile;
import com.gg.common.pojo.SCell;
import com.gg.common.pojo.SPay;
import com.gg.common.pojo.SPayDetails;
import com.gg.hj.search.SPaySearch;
import com.gg.hj.service.SPayService;
import com.gg.hj.util.INumber;
import com.gg.hj.view.SPayDetailsView;
import com.gg.hj.view.SPayDetailsVo;
import com.gg.hj.view.SPayView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SPayServiceImpl implements SPayService {
    @Resource
    private DFileMapper fileMapper;//产品档案
    @Resource
    private SPayMapper sPayMapper;//出库
    @Resource
    private SPayDetailsMapper sPayDetailsMapper;//出库明细
    @Resource
    private INumber iNumber;//流水号
    @Resource
    private SCellMapper sCellMapper;//安全库存
    @Override
    //TODO 出库申请登记
    public List<DFile> queryWarehouseRegistrationService() throws Exception {
        return this.fileMapper.queryWarehouseRegistration();
    }

    @Override
    //TODO 添加出库申请登
    public void saveWarehouseRegistrationService(SPayView sPayView) throws Exception {
        try {
            //设置流水号
            sPayView.getSPay().setPayId(iNumber.number("402"));
            //新增出库
            this.sPayMapper.saveWarehouseRegistration(sPayView.getSPay());
            //设置父级序号
            for (SPayDetails sPayDetails : sPayView.getSPayDetailsList()){
                sPayDetails.setParentId(sPayView.getSPay().getId());
            }
            //新增库存明细
            this.sPayDetailsMapper.saveSPayDetails(sPayView.getSPayDetailsList());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    //TODO 查询出库申请登记审核
    public SPaySearch<SPay> queryWarehouseAuditingService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryWarehouseAuditing());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }

    @Override
    //TODO 根据父级序号查询出库明细
    public List<SPayDetailsVo> queryWarehouseDetailService(int parentId) throws Exception {
        return this.sPayDetailsMapper.queryWarehouseDetail(parentId);
    }

    @Override
    //TODO 审核 通过或不通过
    public int warehouseCheckService(SPay sPay) throws Exception {
        return this.sPayMapper.updateByPrimaryKeySelective(sPay);
    }

    @Override
    //TODO 出库申请查询
    public SPaySearch<SPay> queryWarehouseService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryWarehouse());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }

    @Override
    //TODO 查询审核已通过库存单
    public SPaySearch<SPay> queryWarehouseCheckTagApproveService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryWarehouseCheckTagApprove());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }

    @Override
    //TODO 根据父级序号查询出库明细 制定出库单
    public List<SPayDetails> queryWarehouseDetailById(int parentId) throws Exception {
        return this.sPayDetailsMapper.queryWarehouseDetailById(parentId);
    }

    @Override
    //TODO 查询出库明细 安全库存数量 调度
    public SPayDetailsVo queryWarehouseDispatchDetailService(int id) throws Exception {
        return this.sPayDetailsMapper.queryWarehouseDispatchDetail(id);
    }

    @Override
    //TODO 修改调度产品 已调度
    public int updateWarehouseDispatchDetailService(SPayView sPayView){
        try {
            //修改调度产品为已调度
            this.sPayDetailsMapper.updateByPrimaryKeySelective(sPayView.getSPayDetails());

            List<SPayDetails> sPayDetails1 = this.sPayDetailsMapper.queryByParentIdDispatchProducts(sPayView.getSPay().getId());

            if(sPayDetails1.size()==0){
                //调度产品已调度 修改库存调度状态
                this.sPayMapper.updateByPrimaryKeySelective(sPayView.getSPay());
            }
        }catch(Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    //TODO 查询已调度出库单
    public SPaySearch<SPay> queryWarehouseScheduledService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryWarehouseScheduled());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }

    @Override
    //TODO 根据父级序号查询产品 出库单
    public SPayDetailsView queryOutboundDeliveryOrderService(int id) throws Exception {
        SPayDetailsView sPayDetailsView = new SPayDetailsView();
        //查询产品具有的库存
        sPayDetailsView.setSPayDetailsVos(this.sPayDetailsMapper.queryWarehouseDetail(id));
        //查询产品出库总成本 数量
        sPayDetailsView.setOutMoneyTotal(this.sPayDetailsMapper.queryOutboundCost(id));
        return sPayDetailsView;
    }

    @Override
    //TODO 提交出库单 修改
    public int updateWarehouseScheduledService(SPayView sPayView) throws Exception {
        try {
            //修改库存已登记
            this.sPayMapper.updateByPrimaryKeySelective(sPayView.getSPay());
            //修改库存明细 确定出库数量
            for(SPayDetails s : sPayView.getSPayDetailsList()){
                this.sPayDetailsMapper.updateByPrimaryKeySelective(s);
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    //TODO 查询已登记出库单
    public SPaySearch<SPay> queryWarehouseRegistrationCheckService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryWarehouseRegistrationCheck());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }

    @Override
    //TODO 出库登记复核
    public int updateWarehouseRegistrationCheckService(SPayView sPayView){
        try{
            //修改库存出库数量
            SPay sPay1 = this.sPayMapper.queryByIdOutWarehouse(sPayView.getSPay().getId());
            sPayView.getSPay().setPaidAmountSum(sPay1.getPaidAmountSum()+sPayView.getSPay().getPaidAmountSum());
            this.sPayMapper.updateByPrimaryKeySelective(sPayView.getSPay());

            for(SPayDetails sd : sPayView.getSPayDetailsList()){
                SCell sCell = this.sCellMapper.queryByIdProduct(sd.getProductId());
                sCell.setAmount(sCell.getAmount()-sd.getPaidAmount());
                this.sCellMapper.updateByPrimaryKeySelective(sCell);
                //根据产品id查询产品
                SPayDetails sPayDetails1 = this.sPayDetailsMapper.queryByIdOutWarehouse(sd.getId());
                sd.setPaidAmount(sPayDetails1.getPaidAmount()+sd.getPaidAmount());
                //修改库存明细出库数量
                this.sPayDetailsMapper.updateByPrimaryKeySelective(sd);
                //查询出库明细产品是否已经全部出库
                SPayDetails sPayDetails = this.sPayDetailsMapper.queryByIdOutWarehouse(sd.getId());
                if(sPayDetails==null){
                    sd.setPayTag("K002-2");
                    this.sPayDetailsMapper.updateByPrimaryKeySelective(sd);
                }
            }
            //添加库存明细
            for(SPayDetails save : sPayView.getSavesPayDetailsList()){
                this.sPayDetailsMapper.insertSelective(save);
            }
            //查询出库单是否已经全部出库
            SPay sPay = this.sPayMapper.queryByIdOutWarehouse(sPayView.getSPay().getId());
            if(sPay!=null){
                sPayView.getSPay().setStoreTag("K002-3");
                this.sPayMapper.updateByPrimaryKeySelective(sPayView.getSPay());
            }else{
                SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                sPayView.getSPay().setStoreTag("K002-2");
                sPayView.getSPay().setDateTime(formatter.format(date));
                this.sPayMapper.updateByPrimaryKeySelective(sPayView.getSPay());
            }

        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    //TODO 出库查询
    public SPaySearch<SPay> queryOutWarehouseService(SPaySearch<SPay> sPaySearch) throws Exception {
        PageHelper.startPage(sPaySearch.getPageNum(),sPaySearch.getPageSize());
        PageInfo<SPay> pageInfo = new PageInfo<SPay>(this.sPayMapper.queryOutWarehouse());
        sPaySearch.setTotal(pageInfo.getTotal());
        sPaySearch.setRows(pageInfo.getList());
        return sPaySearch;
    }


}
